﻿@model ProductReviewListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.Catalog.ProductReviews").Text;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
        @T("Admin.Catalog.ProductReviews")
    </div>
    <div class="options">
    <input type="button" id="approve-selected" class="k-button" value="@T("Admin.Catalog.ProductReviews.ApproveSelected")" />
    <input type="button" id="disapprove-selected" class="k-button" value="@T("Admin.Catalog.ProductReviews.DisapproveSelected")" />
    </div>
</div>
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CreatedOnFrom):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CreatedOnFrom)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CreatedOnTo):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CreatedOnTo)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchText):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchText)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="btnSearch" class="k-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
<table class="adminContent">
    <tr>
        <td>
            <div id="productreviews-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#productreviews-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("List", "ProductReview"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        scrollable: false,
                        dataBound: onDataBound,
                        columns: [{
                            field: "Id",
                            headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: "<input type='checkbox' value='#=Id#' class='checkboxGroups'/>",
                            width: 50
                        }, {
                            field: "ProductName",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.Product")",
                            width: 200,
                            template: '<a href="@Url.Content("~/Admin/Product/Edit/")#=ProductId#">#:ProductName#</a>'
                        }, {
                            field: "CustomerId",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.Customer")",
                            width: 200,
                            template: '<a href="@Url.Content("~/Admin/Customer/Edit/")#=CustomerId#">#:CustomerInfo#</a>'
                        }, {
                            field: "Title",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.Title")",
                            width: 200
                        }, {
                            field: "ReviewText",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.ReviewText")",
                            width: 400,
                            encoded: false
                        }, {
                            field: "Rating",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.Rating")",
                            width: 100
                        }, {
                            field: "IsApproved",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.IsApproved")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsApproved#.gif" />'
                        }, {
                            field: "CreatedOn",
                            title: "@T("Admin.Catalog.ProductReviews.Fields.CreatedOn")",
                            width: 200,
                            type: "date",
                            format: "{0:G}"
                        }, {
                            field: "Id",
                            title: "@T("Admin.Common.Edit")",
                            width: 50,
                            template: '<a href="Edit/#=Id#">@T("Admin.Common.Edit")</a>'
                        }]
                    });
                });
            </script>
            <script type="text/javascript">

                var selectedIds = [];

                $(document).ready(function () {

                    //search button
                    $('#btnSearch').click(function () {
                        //search
                        var grid = $('#productreviews-grid').data('kendoGrid');
                        grid.dataSource.page(1); //new search. Set page size to 1
                        //grid.dataSource.read(); we already loaded the grid above using "page" function
                        //clear selected checkboxes
                        $('.checkboxGroups').attr('checked', false).change();
                        selectedIds = [];
                        return false;
                    });

                    //"approve selected" button
                    $('#approve-selected').click(function (e) {
                        e.preventDefault();

                        var postData = {
                            selectedIds: selectedIds
                        };

                        $.ajax({
                            cache:false,
                            type: "POST",
                            url: "@(Url.Action("ApproveSelected", "ProductReview"))",
                            data: postData,
                            complete: function (data) {
                                //reload grid
                                var grid = $('#productreviews-grid').data('kendoGrid');
                                grid.dataSource.read();
                            },
                            error:function (xhr, ajaxOptions, thrownError){
                                alert(thrownError);
                            },
                            traditional: true
                        });
                        return false;
                    });

                    //"disapprove selected" button
                    $('#disapprove-selected').click(function (e) {
                        e.preventDefault();

                        var postData = {
                            selectedIds: selectedIds
                        };

                        $.ajax({
                            cache:false,
                            type: "POST",
                            url: "@(Url.Action("DisapproveSelected", "ProductReview"))",
                            data: postData,
                            complete: function (data) {
                                //reload grid
                                var grid = $('#productreviews-grid').data('kendoGrid');
                                grid.dataSource.read();
                            },
                            error:function (xhr, ajaxOptions, thrownError){
                                alert(thrownError);
                            },
                            traditional: true
                        });
                        return false;
                    });


                    $('#mastercheckbox').click(function () {
                        $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                    });

                    //wire up checkboxes.
                    $('#productreviews-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                        var $check = $(this);
                        if ($check.is(":checked") == true) {
                            var checked = jQuery.inArray($check.val(), selectedIds);
                            if (checked == -1) {
                                //add id to selectedIds.
                                selectedIds.push($check.val());
                            }
                        }
                        else {
                            var checked = jQuery.inArray($check.val(), selectedIds);
                            if (checked > -1) {
                                //remove id from selectedIds.
                                selectedIds = $.grep(selectedIds, function (item, index) {
                                    return item != $check.val();
                                });
                            }
                        }
                        updateMasterCheckbox();
                    });
                });

                function additionalData() {
                    return {
                        CreatedOnFrom: $('#@Html.FieldIdFor(model => model.CreatedOnFrom)').val(),
                        CreatedOnTo: $('#@Html.FieldIdFor(model => model.CreatedOnTo)').val(),
                        SearchText: $('#@Html.FieldIdFor(model => model.SearchText)').val()
                    };
                }

                function onDataBound(e) {

                    $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                        var currentId = $(this).val();
                        var checked = jQuery.inArray(currentId, selectedIds);
                        //set checked based on if current checkbox's value is in selectedIds.
                        $(this).attr('checked', checked > -1);
                    });

                    updateMasterCheckbox();
                }

                function updateMasterCheckbox() {
                    var numChkBoxes = $('#productreviews-grid input[type=checkbox][id!=mastercheckbox]').length;
                    var numChkBoxesChecked = $('#productreviews-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
                    $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                }

            </script>
        </td>
    </tr>
</table>

